﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Game
{
    namespace RenderTechnique
    {
        class ShadowRenderTechnique : OutputTechnique
        {
            private IDK.SunCamera mSunCamera;
            private IDK.LightMatrix mSunShadowLightMatrix;
            private IDK.RenderTarget mRttShadow;

            public ShadowRenderTechnique(TechniqueDescription desc)
                : base(desc)
            {
            }

            public IDK.LightMatrix SunShadowLightMatrix
            {
                get { return mSunShadowLightMatrix; }
            }

            public IDK.RenderTarget RTTShadow
            {
                get { return mRttShadow; }
            }

            public IDK.Camera InitSunShadow()
            {
                //add light matrix
                mSunShadowLightMatrix = new IDK.LightMatrix();

                //add sun cam
                mSunCamera = new IDK.SunCamera(Description.Camera);

                //add sun shadow rtt
                mRttShadow = Description.Core.GetRenderTarget("rttShadow");
                mRttShadow.AttachTexture(Description.Core.CreateRenderTexture(512, 512, IDK.Texture.Format.RGBA, IDK.Texture.PixelType.FloatRGBA16));

                // shadow program
                IDK.ShaderProgram programShadow = Description.Core.GetShaderProgram("Shadow");
                programShadow.AddParameter(IDK.ShaderProgram.Parameter.Type.VertexAttribute, IDK.ShaderProgram.Parameter.Declaration.VertexPosition, "position");

                programShadow.AddParameter(IDK.ShaderProgram.Parameter.Type.ConstantParameter, IDK.ShaderProgram.Parameter.Declaration.MatrixProjection, "gProj");
                programShadow.AddParameter(IDK.ShaderProgram.Parameter.Type.ConstantParameter, IDK.ShaderProgram.Parameter.Declaration.MatrixView, "gView");
                programShadow.AddParameter(IDK.ShaderProgram.Parameter.Type.ConstantParameter, IDK.ShaderProgram.Parameter.Declaration.MatrixWorld, "gWorld");

                
                //attach object fake proxy
                Description.Renderer.AddPass(new IDK.SunShadowPass(Description.Core.GetScene(), mSunCamera, Description.Camera, programShadow, mRttShadow, mSunShadowLightMatrix));
                return mSunCamera;
            }

            public override void Update(Events.ProcessSceneUpdate evnt)
            {
                //Update Sun Camera
                mSunCamera.Update(evnt.ElapsedTime);
            }
        }
    }
}
